package bTree.LeetCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class 找到字符串中所有字母异位词 {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> res = new ArrayList<>();
        if (p.length() >s.length()) {
            return res;
        }
        int[] cntS = new int[26];
        int[] cntP = new int[26];
        for (int i = 0; i < p.length(); i++) {
            cntP[p.charAt(i) - 'a']++;
        }
        for (int i = 0; i < p.length() - 1; i++) {
            cntS[s.charAt(i) - 'a']++;
        }
        int left = 0;
        for (int right = p.length() - 1; right < s.length(); right++) {
            cntS[s.charAt(right) - 'a']++;
            if (Arrays.equals(cntP, cntS)) {
                res.add(left);
            }
            cntS[s.charAt(left) - 'a']--;
            left++;
        }
        return res;
    }
}
